package com.jy.adscm.operation.hscrb.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.operation.hscrb.bean.ZjkMiddlelibrary;
import com.jy.adscm.operation.hscrb.bean.ZjkMiddlelibrary;
import com.jy.adscm.operation.hscrb.mapper.mysql.ZjkMiddlelibraryMapper;
import com.jy.adscm.operation.hscrb.service.ZjkMiddlelibraryService;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2018-12-17
 */
@Service
public class ZjkMiddlelibraryServiceImpl extends ServiceImpl<ZjkMiddlelibraryMapper, ZjkMiddlelibrary> implements ZjkMiddlelibraryService {
    @Autowired
    JyUserMapper jyUserMapper;
    @Override
    public Boolean updateZjkMiddlelibrary(ZjkMiddlelibrary zjkMiddlelibrary, Token token) {
        //待办修改
        if (zjkMiddlelibrary.getTaskstate()==0 && zjkMiddlelibrary.getTaskfinishstate()==0 &&
                "0".equals(zjkMiddlelibrary.getIstaskstart()) &&  "0".equals(zjkMiddlelibrary.getIstaskbegin()) &&
                "0".equals(zjkMiddlelibrary.getIstaskend())  && "0".equals(zjkMiddlelibrary.getIsreviewtask())
        ){


            return  super.updateById(zjkMiddlelibrary);
        }
        //接收任务
        if (zjkMiddlelibrary.getTaskstate()==0 && zjkMiddlelibrary.getTaskfinishstate()==0 &&
                "1".equals(zjkMiddlelibrary.getIstaskstart()) &&  "0".equals(zjkMiddlelibrary.getIstaskbegin()) &&
                "0".equals(zjkMiddlelibrary.getIstaskend())  && "0".equals(zjkMiddlelibrary.getIsreviewtask())
        ){
            zjkMiddlelibrary.setIstaskstart("1");
            zjkMiddlelibrary.setTaskstartaddtime(new Date());
            zjkMiddlelibrary.setTaskstate(1);

            return  super.updateById(zjkMiddlelibrary);
        }
        //开始任务
        if (zjkMiddlelibrary.getTaskstate()==1 && zjkMiddlelibrary.getTaskfinishstate()==0 &&
                "1".equals(zjkMiddlelibrary.getIstaskstart()) &&  "1".equals(zjkMiddlelibrary.getIstaskbegin()) &&
                "0".equals(zjkMiddlelibrary.getIstaskend())  && "0".equals(zjkMiddlelibrary.getIsreviewtask())
        ){
            zjkMiddlelibrary.setIstaskbegin("1");
            zjkMiddlelibrary.setTaskbeginaddtime(new Date());
            return super.updateById(zjkMiddlelibrary);
        }
        //结束任务
        if (zjkMiddlelibrary.getTaskstate()==1 && zjkMiddlelibrary.getTaskfinishstate()==0 &&
                "1".equals(zjkMiddlelibrary.getIstaskstart()) &&  "1".equals(zjkMiddlelibrary.getIstaskbegin()) &&
                "1".equals(zjkMiddlelibrary.getIstaskend())  && "0".equals(zjkMiddlelibrary.getIsreviewtask())
        ){
            zjkMiddlelibrary.setIstaskend("1");
            zjkMiddlelibrary.setTaskfinishstate(1);
            zjkMiddlelibrary.setTaskendaddtime(new Date());
            return super.updateById(zjkMiddlelibrary);
        }
        //复核任务
        if (zjkMiddlelibrary.getTaskstate()==1 && zjkMiddlelibrary.getTaskfinishstate()==1 &&
                "1".equals(zjkMiddlelibrary.getIstaskstart()) &&  "1".equals(zjkMiddlelibrary.getIstaskbegin()) &&
                "1".equals(zjkMiddlelibrary.getIstaskend())  && "1".equals(zjkMiddlelibrary.getIsreviewtask())
        ){
            zjkMiddlelibrary.setIsreviewtask("1");
            zjkMiddlelibrary.setReviewaddtime(new Date());
            zjkMiddlelibrary.setReviewperson(token.getUser().getId().toString());
            return super.updateById(zjkMiddlelibrary);
        }

        return null;
    }

    @Override
    public List<ZjkMiddlelibrary> selectZjkMiddlelibrary(Integer taskState, Integer taskFinishState, Integer isReviewTask) {
        Wrapper<ZjkMiddlelibrary> wrapper =new EntityWrapper<>();
        wrapper.eq("TASKSTATE",taskState);
        wrapper.eq("TASKFINISHSTATE",taskFinishState);
        wrapper.eq("isReviewTask",isReviewTask);
        List<ZjkMiddlelibrary> zjkMiddlelibrarys= super.selectList(wrapper);



        for (ZjkMiddlelibrary zjkMiddlelibrary : zjkMiddlelibrarys) {
            if ( zjkMiddlelibrary.getTaskreleaseperson()!=null && !"".equals( zjkMiddlelibrary.getTaskreleaseperson())){
                JyUser jyUser = jyUserMapper.selectById(zjkMiddlelibrary.getTaskreleaseperson());
                zjkMiddlelibrary.setTaskreleasepersonName(jyUser.getName());
            }
            if ( zjkMiddlelibrary.getTaskperformperson()!=null && !"".equals( zjkMiddlelibrary.getTaskperformperson())){
                JyUser jyUser = jyUserMapper.selectById(zjkMiddlelibrary.getTaskperformperson());
                zjkMiddlelibrary.setTaskperformpersonName(jyUser.getName());
            }

            if ( zjkMiddlelibrary.getReviewperson()!=null && !"".equals( zjkMiddlelibrary.getReviewperson())){
                JyUser jyUser = jyUserMapper.selectById(zjkMiddlelibrary.getReviewperson());
                zjkMiddlelibrary.setReviewpersonName(jyUser.getName());
            }

        }

        return zjkMiddlelibrarys;
    }

    @Override
    public List<JyUser> selectTaskPerformPerson() {
        List<JyUser> jyUsers = jyUserMapper.selectTaskPerformPerson("111");
        return jyUsers;
    }
}
